package com.tech.koufu.ui.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.tech.koufu.R;
import com.tech.koufu.model.StockInfo;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.activity.BuyStocksActivity;
import com.tech.koufu.ui.activity.DetailTransStocksActivity;
import com.tech.koufu.ui.activity.MarketDeatilActivity;  
import android.content.Context;
import android.content.Intent; 
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan; 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; 

/**
 *
 * @ClassName: PostionsStocksAdapter
 * @Description: 我的持仓适配器
 * @author 123
 * @date 2015年4月5日 上午11:37:39
 */
public class PostionsStocksAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater mInflater;
	public ArrayList<UserStocks> mUserStocks ;
//	public ArrayList<StockInfo> mStockInfos; 
	public int cuspos; 
	public PostionsStocksAdapter(Context context){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() { 
		if(mUserStocks!=null)
			return mUserStocks.size();
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		if(mUserStocks!=null){
			return mUserStocks.get(position);
		} 
		return null;
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) { 
		ViewHolder holder = null;   
        if(convertView == null)  
        {  
            holder = new ViewHolder();   
            convertView = mInflater.inflate(R.layout.lv_item_positions, parent,false);  
            holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);
            holder.txt_positions_value = (TextView)convertView.findViewById(R.id.txt_positions_value);
            holder.txt_breakevenamount = (TextView)convertView.findViewById(R.id.txt_breakevenamount);
            holder.txt_curprice = (TextView)convertView.findViewById(R.id.txt_curprice); 
            
            holder.txt_cost = (TextView)convertView.findViewById(R.id.txt_cost);  
            holder.txt_stock_num = (TextView)convertView.findViewById(R.id.txt_stock_num); 
            holder.txt_stock_market = (TextView)convertView.findViewById(R.id.txt_stock_market); 
            
            holder.ly_pos_ttoggle = (LinearLayout)convertView.findViewById(R.id.ly_pos_ttoggle);
            holder.ly_open = (LinearLayout)convertView.findViewById(R.id.ly_open);
            
            holder.btnBuy = (Button)convertView.findViewById(R.id.btn_buy);
            holder.btnSell = (Button)convertView.findViewById(R.id.btn_sell);
            holder.btn_trans_details = (Button)convertView.findViewById(R.id.btn_trans_details);
            holder.btn_market = (Button)convertView.findViewById(R.id.btn_market);
            holder.togView = (ImageView)convertView.findViewById(R.id.btn_iv_toggle); 
            convertView.setTag(holder);  
        }else  
        {  
            holder = (ViewHolder)convertView.getTag();  
        }  
      
        ItemClick onclick =  new ItemClick(position);
        holder.btnBuy.setOnClickListener(onclick);
        holder.btnSell.setOnClickListener(onclick);
        holder.btn_trans_details.setOnClickListener(onclick);
        holder.btn_market.setOnClickListener(onclick);
        UserStocks info = (UserStocks)getItem(position);
        //盈亏金额
        Float breakevenamount_price = 0f;
		Float charge = 0f;
		Float stock_market_price = 0f;
		String ykl = "",ykje = "",zdl = "";
		String dqj = info.getStockdqj();
		try {
			 holder.txt_stock_name.setText(info.getStockName()); 
		     holder.ly_pos_ttoggle.setVisibility(View.GONE);
		     String zsp = info.getStockZsp();Log.e("TTT",zsp+"~~~~~~~~~~~");
			 if(zsp==null || (zsp!=null && zsp.equals(KouFuTools.TAG_OFF_STOCK))){ 
			      holder.txt_positions_value.setText(String.format(this.context.getResources().getString(R.string.txt_item_mycount), KouFuTools.TAG_OFF_STOCK));  
			    
				 holder.txt_breakevenamount.setText(String.format(this.context.getResources().getString(R.string.lv_item_breakevenamount_value), KouFuTools.TAG_OFF_STOCK,KouFuTools.TAG_OFF_STOCK)); 
				 holder.txt_curprice.setText(String.format(this.context.getResources().getString(R.string.lv_item_curprice_value), KouFuTools.TAG_OFF_STOCK,KouFuTools.TAG_OFF_STOCK));
				 holder.txt_cost.setText(String.format(this.context.getResources().getString(R.string.lv_item_cost_vlaue), KouFuTools.TAG_OFF_STOCK)); 
				 holder.txt_stock_num.setText(String.format(this.context.getResources().getString(R.string.lv_item_stock_num_value), KouFuTools.TAG_OFF_STOCK)); 
				 holder.txt_stock_market.setText(String.format(this.context.getResources().getString(R.string.lv_item_stock_market_value), KouFuTools.TAG_OFF_STOCK)); 
			 }else{
				 DecimalFormat decimalFormat=new DecimalFormat("##0.00");
					breakevenamount_price = Float.parseFloat(info.getStockdqj()) * Float.parseFloat(info.getStockHold()) - 
						 Float.parseFloat(info.getStockykj()) * Float.parseFloat(info.getStockHold());
					ykje = decimalFormat.format(breakevenamount_price);
				    ykl = decimalFormat.format (breakevenamount_price/(Float.parseFloat(info.getStockykj()) *  Float.parseFloat(info.getStockHold())) *100)+"%";
					//当前价 涨跌率
				    zsp = zsp!=null ? info.getStockZsp():"0"; 
				    zdl = decimalFormat.format ( (Float.parseFloat(dqj) - Float.parseFloat(zsp)) / Float.parseFloat(zsp) * 100 ) +"%";
				    
					charge = Float.parseFloat(info.getStockdqj()) * Float.parseFloat(info.getStockHold()) / 
						 Float.parseFloat(info.getAllmoney()) * 100;
					stock_market_price = Float.parseFloat(info.getStockdqj()) * Float.parseFloat(info.getStockHold());
			
		        
			       //仓位
			        String c =KouFuTools.df2.format(charge) ;
			        c = String.format(this.context.getResources().getString(R.string.txt_item_mycount), c+"%");
			        SpannableStringBuilder style=new SpannableStringBuilder(c);          
			        style.setSpan(new ForegroundColorSpan(this.context.getResources().getColor(R.color.bgBlackColor_63)),c.length()-1,c.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
			        holder.txt_positions_value.setText(style);  
			         
			        //盈亏金额
			//        String p1 = "40000.0",p2 = "40.00%";
			        String p = String.format(this.context.getResources().getString(R.string.lv_item_breakevenamount_value), ykje,ykl); 
			        SpannableString style1=new SpannableString(p);         
			        style1.setSpan(new ForegroundColorSpan(context.getResources().getColor(KouFuTools.getStockValueColor(ykje))),5,5+ykje.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			        style1.setSpan(new ForegroundColorSpan(context.getResources().getColor(KouFuTools.getStockValueColor(ykl))),5+ykje.length()+1,p.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
			        holder.txt_breakevenamount.setText(style1);  //,BufferType.SPANNABLE
			        //当前价
			        p = String.format(this.context.getResources().getString(R.string.lv_item_curprice_value), info.getStockdqj(),zdl);
			        style1=new SpannableString(p);         
			        style1.setSpan(new ForegroundColorSpan(context.getResources().getColor(KouFuTools.getStockValueColor(zdl))),p.length()-dqj.length()-zdl.length()-2,p.length()-zdl.length()-2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			        style1.setSpan(new ForegroundColorSpan(context.getResources().getColor(KouFuTools.getStockValueColor(zdl))),p.length()-zdl.length()-1,p.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);         
			        holder.txt_curprice.setText(style1);
			        
			        //成本价
			        p = String.format(this.context.getResources().getString(R.string.lv_item_cost_vlaue), info.getStockbj());
			        style1=new SpannableString(p);         
			        style1.setSpan(new ForegroundColorSpan(this.context.getResources().getColor(R.color.TextColorBlack)),p.length()-info.getStockbj().length()-1,p.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			        holder.txt_cost.setText(style1); 
			        //数量
			        p = String.format(this.context.getResources().getString(R.string.lv_item_stock_num_value), info.getStockHold());
			        style1=new SpannableString(p);         
			        style1.setSpan(new ForegroundColorSpan(this.context.getResources().getColor(R.color.TextColorBlack)),p.length()-info.getStockHold().length()-1,p.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			        holder.txt_stock_num.setText(style1); 
			        //市值
			        p = String.format(this.context.getResources().getString(R.string.lv_item_stock_market_value), stock_market_price);
			        style1=new SpannableString(p);         
			        style1.setSpan(new ForegroundColorSpan(this.context.getResources().getColor(R.color.TextColorBlack)),p.length()-(stock_market_price+"").length()-1,p.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			        holder.txt_stock_market.setText(style1); 
			 }
			
        
		} catch (NumberFormatException e) { 
			e.printStackTrace();
		}catch(NullPointerException e){
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
        final View openContent = holder.ly_pos_ttoggle;
        final ImageView m_togView = holder.togView;
        holder.ly_open.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) { 
				if(openContent.getVisibility() == View.VISIBLE){
					openContent.setVisibility(View.GONE);
					m_togView.setImageResource(R.drawable.btn_lv_open);
				}else{
					openContent.setVisibility(View.VISIBLE);
					m_togView.setImageResource(R.drawable.btn_lv_close);
				}
			}
		});
		return convertView;
	}
	static class ViewHolder  
   {    
       public TextView txt_stock_name;  
       public TextView txt_positions_value;  
       public TextView txt_breakevenamount;  
       public TextView txt_curprice;   
       public LinearLayout ly_pos_ttoggle,ly_open;
       public TextView txt_cost; 
       public TextView txt_stock_num; 
       public TextView txt_stock_market;  
       public Button btnBuy,btnSell,btn_market,btn_trans_details;
       public ImageView togView;
       
       
   }  
	 class ItemClick implements View.OnClickListener{
		 private int pos;
		public ItemClick(int pos){
			this.pos = pos;
		}
		@Override
		public void onClick(View v) { 
			UserStocks ustock = (UserStocks)getItem(pos);
			switch (v.getId()) {
			case R.id.btn_buy:
				Intent intent = new Intent(context, BuyStocksActivity.class);
//				intent.putExtra("stockInfo", datas.get(position));
				intent.putExtra("code", ustock.getOrigStockId());
				intent.putExtra("name", ustock.getStockName());
				intent.putExtra("userStocks", mUserStocks);
				intent.putExtra("datatype", 1);
				intent.putExtra("parsetype",3);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;
			case R.id.btn_sell:
				intent = new Intent(context, BuyStocksActivity.class);
				intent.putExtra("stockInfo",	ustock);
				intent.putExtra("userStocks", mUserStocks);
				intent.putExtra("type", 1);  
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;
			case R.id.btn_trans_details:
				intent = new Intent(context,DetailTransStocksActivity.class);
				intent.putExtra("userStock", ustock);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;
			case R.id.btn_market:
				intent = new Intent(context,MarketDeatilActivity.class);
				intent.putExtra("stockCode",ustock.stockId);
				intent.putExtra("stockName",ustock.stockName);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;  
			default:
				break;
			}
		}
		
	}
 
}
